Here are some basic syntax elements of the Java programming language:
publicclassCodeGeek{ publicstaticvoidmain(String[] args) { System.out.println("Hello Coders!"); } }
The name of the file has to be the class name. Example : Here class name is CodeGeek therefore filename must have name as CodeGeek. Otherwise it will throw error.
Comments are used to add notes and explanations to your code. In Java, there are two types of comments: single-line comments and multi-line comments. Single-line comments start with // and extend to the end of the line. Multi-line comments start with /* and end with */ . Example
// This is a single-line comment/*This is amulti-line comment*/
Variables are used to store values in a program. In Java, you must declare a variable before you can use it and specify its data type. For example:
intx; // Declare an integer variable named xx = 10; // Assign the value 10 to xStringname; // Declare a string variable named namename = "John"; // Assign the value "John" to name
Java has several built-in data types, including integers (int), floating-point numbers (float and double), characters (char), and boolean values (boolean). For example:
intx = 10; // Integerfloatpi = 3.14; // Floating-point numbercharletter = 'A'; // Characterbooleanflag = true; // Boolean value
Java is a case-sensitive language, which means that the language treats identifiers (such as variables, methods, and class names) that are written with different capitalization as different. For example, the variables x
and X
are treated as different identifiers in Java. The following code will result in an error:
intx = 5; System.out.println(X);
Output:
error: cannotfindsymbolSystem.out.println(X); ^ symbol: variableXlocation: classMain